﻿using System;
using System.Collections.Generic;

using System.Text;

namespace RNT.Common.Utils
{
    /// <summary>
    /// 小写金额转换为大写金额，金额小于壹万亿，允许两位小数
    /// </summary>
    public class DecimalToUpper
    {
        /// <summary>
        /// 允许最大的金额长度
        /// </summary>
        int DECIMAL_MAX_LEN = 15;

        /// <summary>
        /// 零
        /// </summary>
        char LING = '零';
        /// <summary>
        /// 拾
        /// </summary>
        char SHI = '拾';
        /// <summary>
        /// 佰
        /// </summary>
        char BAI = '佰';
        /// <summary>
        /// 仟
        /// </summary>
        char QIAN = '仟';
        /// <summary>
        /// 万
        /// </summary>
        char WAN = '万';
        /// <summary>
        /// 亿
        /// </summary>
        char YI = '亿';
        /// <summary>
        /// 元
        /// </summary>
        char YUAN = '元';
        /// <summary>
        /// 角
        /// </summary>
        char JIAO = '角';
        /// <summary>
        /// 分
        /// </summary>
        char FEN = '分';
        /// <summary>
        /// 整
        /// </summary>
        char ZHENG = '整';
        /// <summary>
        /// 零元整
        /// </summary>
        string ZERO = "零元整";

        /// <summary>
        /// 将小写金额转换为大写金额
        /// </summary>
        /// <param name="money">源金额，允许两位小数 </param>
        /// <returns>大写金额</returns>
        public string ToUpper(decimal money)
        {
            if (money == 0)
            {
                return ZERO;
            }

            string moneyFormat = money.ToString("####.00");

            if (moneyFormat.Length > DECIMAL_MAX_LEN)
            {
                throw new ArgumentException("超出计算范围");
            }

            //若小于 DECIMAL_MAX_LEN 位长，前面补足 0
            moneyFormat = new String('0', DECIMAL_MAX_LEN - moneyFormat.Length) + moneyFormat;

            string YI_PART = moneyFormat.Substring(0, 4);			//取得'亿'单元
            string WAN_PART = moneyFormat.Substring(4, 4);			//取得'万'单元
            string YUAN_PART = moneyFormat.Substring(8, 4);			//取得'元'单元
            string FLOAT_PART = moneyFormat.Substring(13, 2);		//取得小数部分
            
            string YI_UPPER = "", WAN_UPPER = "", YUAN_UPPER = "";

            YI_UPPER = this.GetUpper(YI_PART, YI);					//亿单元的大写
            WAN_UPPER = this.GetUpper(WAN_PART, WAN);				//万单元的大写
            YUAN_UPPER = this.GetUpper(YUAN_PART, YUAN);			//元单元的大写


            string str_y = "", str_w = "";
            if (moneyFormat[3] == '0' || moneyFormat[4] == '0')		//亿和万之间是否有'零'
            {
                str_y = LING.ToString();
            }
            if (moneyFormat[7] == '0' || moneyFormat[8] == '0')		//万和元之间是否有'零'
            {
                str_w = LING.ToString();
            }

            //亿，万，元的三个大写合并
            string ret = YI_UPPER + str_y + WAN_UPPER + str_w + YUAN_UPPER;

            for (int i = 0; i < ret.Length; i++)				    //去掉前面的'零'			
            {
                if (ret[i] != LING)
                {
                    ret = ret.Substring(i);
                    break;
                }

            }

            for (int i = ret.Length - 1; i > -1; i--)				//去掉最后的'零'	
            {
                if (ret[i] != LING)
                {
                    ret = ret.Substring(0, i + 1);
                    break;
                }
            }

            if (ret[ret.Length - 1] != YUAN)							//若最后不位不是'元'，则加一个'元'字
            {
                ret = ret + YUAN.ToString();
            }

            if (ret == "零零元")										//若为零元，则去掉"元数"，结果只要小数部分
            {
                ret = "";
            }

            if (FLOAT_PART == "00")									//下面是小数部分的转换
            {
                ret = ret + LING;
            }
            else
            {
                string tmp = "";
                tmp = this.UpperNumber(FLOAT_PART[0]);
                if (tmp == LING.ToString())
                {
                    ret = ret + tmp;
                }
                else
                {
                    ret = ret + tmp + JIAO.ToString();
                }

                tmp = this.UpperNumber(FLOAT_PART[1]);
                if (tmp == LING.ToString())
                {
                    ret = ret + ZHENG.ToString();
                }
                else
                {
                    ret = ret + tmp + FEN.ToString();
                }
            }

            if (ret[0] == LING)
            {
                ret = ret.Substring(1);		//防止0.03转为"零叁分"，而直接转为"叁分"
            }

            return ret;			    		//完成，返回


        }
        /// <summary>
        /// 把一个单元转为大写，如亿单元，万单元，个单元
        /// </summary>
        /// <param name="numbers">这个单元的小写数字（4位长，若不足，则前面补零）</param>
        /// <param name="unit">单位（亿，万，元）</param>
        /// <returns>转换结果</returns>
        private string GetUpper(String numbers, char unit)
        {
            if (numbers == "0000")
            {
                return String.Empty;
            }

            string tmp = String.Empty;
            string tmp1 = this.UpperNumber(numbers[0]);
            string tmp2 = this.UpperNumber(numbers[1]);
            string tmp3 = this.UpperNumber(numbers[2]);
            string tmp4 = this.UpperNumber(numbers[3]);

            if (tmp1 != LING.ToString())
            {
                tmp = tmp + tmp1 + QIAN;
            }
            else
            {
                tmp = tmp + tmp1;
            }

            if (tmp2 != LING.ToString())
            {
                tmp = tmp + tmp2 + BAI;
            }
            else
            {
                //保证若有两个零'00'，结果只有一个零，下同
                if (tmp1 != LING.ToString())
                {
                    tmp = tmp + tmp2;
                }
            }

            if (tmp3 != LING.ToString())
            {
                tmp = tmp + tmp3 + SHI;
            }
            else
            {
                if (tmp2 != LING.ToString())
                {
                    tmp = tmp + tmp3;
                }
            }

            if (tmp4 != LING.ToString())
            {
                tmp = tmp + tmp4;
            }

            //若第一个字符是'零'，则去掉
            if (tmp[0] == LING)
            {
                tmp = tmp.Substring(1);
            }

            //若最后一个字符是'零'，则去掉
            if (tmp[tmp.Length - 1] == LING)
            {
                tmp = tmp.Substring(0, tmp.Length - 1);
            }

            return tmp + unit;	//加上本单元的单位

        }

        /// <summary>
        /// 将数字转为大写
        /// </summary>
        /// <param name="number">数字 0~9</param>
        public string UpperNumber(char number)
        {
            string intval = null;

            try
            {
                intval = Convert.ToInt32(number).ToString();
            }
            catch
            {
                throw new ArgumentException();
            }

            switch (number)
            {
                case '0':
                    intval = "零";
                    break;
                case '1':
                    intval = "壹";
                    break;
                case '2':
                    intval = "贰";
                    break;
                case '3':
                    intval = "叁";
                    break;
                case '4':
                    intval = "肆";
                    break;
                case '5':
                    intval = "伍";
                    break;
                case '6':
                    intval = "陆";
                    break;
                case '7':
                    intval = "柒";
                    break;
                case '8':
                    intval = "拐";
                    break;
                case '9':
                    intval = "玖";
                    break;
            }
            return intval;
        }
    }
}
